package com.raja.requestForwarding.controller;

import com.raja.requestForwarding.utils.LocaleMessageSourceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * Created by dell on 2017/4/27.
 */
@Controller
public class Il8Controller {

    @Autowired
    private MessageSource messageSource;

    @RequestMapping(value = "/il8")
    public String index(HttpServletRequest request, HttpServletResponse response){
        Locale locale = LocaleContextHolder.getLocale();
        String msg = messageSource.getMessage("welcome", null,locale);
        System.out.println(msg);
        return "il8/index";
    }
    @Autowired
    private LocaleMessageSourceUtil messageSourceUtil;

    @RequestMapping(value = "/hello")
    public String hello(HttpServletRequest request, HttpServletResponse response){
        String welcome = messageSourceUtil.getMessage("welcome");
        System.out.println(welcome);
        return "il8/index";
    }

    @RequestMapping("/changeSessionLanauage")
    public String changeSessionLanauage(HttpServletRequest request, HttpServletResponse response,
                                        String lang){
        System.out.println(lang);
        LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
        if("zh".equals(lang)){
            localeResolver.setLocale(request, response, new Locale("zh", "CN"));
        }else if("en".equals(lang)){
            localeResolver.setLocale(request, response, new Locale("en", "US"));
        }
        return "redirect:/hello";
    }
}
